﻿#include "calendar.h"
#include "ui_calendar.h"

Calendar::Calendar(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Calendar)
{
    ui->setupUi(this);
    displaysetting = new DisplaySetting(this);
    connect(displaysetting,&DisplaySetting::sendImage,this,&Calendar::ImagesReceive);
    displaysetting->close();
    defaultImage.load(":/four_seasons/scenery_8.PNG");
    ui->label_showLandscape->setPixmap(defaultImage);

    speech = new QTextToSpeech(this);

    playlist = new QMediaPlaylist(this);
    player = new QMediaPlayer(this);
    playlist->addMedia(QUrl("qrc:/img/music_bg.mp3"));  //原曲：前世今生(钢琴曲)
    playlist->addMedia(QUrl("qrc:/img/spring.mp3"));    //原曲：空
    playlist->addMedia(QUrl("qrc:/img/summer.mp3"));    //原曲：那年夏天，阳光正好
    playlist->addMedia(QUrl("qrc:/img/autumn.mp3"));    //原曲：妖艳
    playlist->addMedia(QUrl("qrc:/img/winter.mp3"));    //原曲：冬雪
    playlist->setPlaybackMode(QMediaPlaylist::CurrentItemOnce);
    player->setVolume(50);
    player->setPlaylist(playlist);
    player->play();
}

Calendar::~Calendar()
{
    delete ui;
}

void Calendar::ImagesReceive(QList<LandscapeImage*> images,int type)
{
    //清空原来存储的图片
    Spring.clear();
    Summer.clear();
    Autumn.clear();
    Winter.clear();
    Images.clear();

    Images = images;
    ruleType = type;

    for (int i=0;i<images.count();i++) {
        switch(images.at(i)->ImageIndex){
        case 0:
            Spring.append(images.at(i));
            break;
        case 1:
            Summer.append(images.at(i));
            break;
        case 2:
            Autumn.append(images.at(i));
            break;
        case 3:
            Winter.append(images.at(i));
            break;
        }
    }
}

/*显示按钮点击事件*/
void Calendar::on_pushButton_set_clicked()
{
    displaysetting->show();
}

/*日历点击事件*/
void Calendar::on_calendarWidget_clicked(const QDate &date)
{ 
    QString selected_date = QString::number(date.year())+"年"+QString::number(date.month())+"月"+QString::number(date.day())+"日";
    //至少有选择图片，否则不显示
    if(!Images.isEmpty())
    {
        /*图片选择按四季或随机显示*/
        if(ruleType==0)
       {
            //春季为3、4、5月份，夏季为6、7、8月份，秋季为9、10、11月份，冬季为12、1、2月份。
            if(date.month()>=3 && date.month()<=5)  //春天
            {
                //背景音乐更换
                if(playlist->currentIndex()!=1)
                {
                  player->setVolume(15);
                  playlist->setCurrentIndex(1);
                  player->play();
                }
                //语音播报
                if(speech->state()==QTextToSpeech::Ready)
                {
                  speech->say("您所选择的时间是"+selected_date+"，正直春天，请注意锻炼身体");
                }
                //显示春天图片
                if(Spring.isEmpty())
                    ui->label_showLandscape->setPixmap(defaultImage);
                else
                    ui->label_showLandscape->setPixmap(Spring.at(date.day()%Spring.count())->ImagePath);
            }
            else if(date.month()>=6 && date.month()<=8) //夏天
            {
                //背景音乐更换
                if(playlist->currentIndex()!=2)
                {
                  player->setVolume(20);
                  playlist->setCurrentIndex(2);
                  player->play();
                }
                //语音播报
                if(speech->state()==QTextToSpeech::Ready)
                {
                  speech->say("您所选择的时间是"+selected_date+"，正直夏天，注意天气炎热，预防中暑");
                }
                //显示夏天图片
                if(Summer.isEmpty())
                    ui->label_showLandscape->setPixmap(defaultImage);
                else
                    ui->label_showLandscape->setPixmap(Summer.at(date.day()%Summer.count())->ImagePath);

            }
            else if(date.month()>=9 && date.month()<=11)    //秋天
            {
                //背景音乐更换
                if(playlist->currentIndex()!=3)
                {
                  player->setVolume(20);
                  playlist->setCurrentIndex(3);
                  player->play();
                }
                //语音播报
                if(speech->state()==QTextToSpeech::Ready)
                {
                  speech->say("您所选择的时间是"+selected_date+"，正直秋天，请注意天气更换，做好防寒保暖");
                }
                //显示秋天图片
                if(Autumn.isEmpty())
                    ui->label_showLandscape->setPixmap(defaultImage);
                else
                    ui->label_showLandscape->setPixmap(Autumn.at(date.day()%Autumn.count())->ImagePath);

            }
            else if(date.month()>=12 || date.month()<=2)    //冬天
            {
                //背景音乐更换
                if(playlist->currentIndex()!=4)
                {
                  player->setVolume(15);
                  playlist->setCurrentIndex(4);
                  player->play();
                }
                //语音播报
                if(speech->state()==QTextToSpeech::Ready)
                {
                  speech->say("您所选择的时间是"+selected_date+"，正直冬天，天气寒冷，请做好保暖措施");
                }
                //显示冬天图片
                if(Winter.isEmpty())
                    ui->label_showLandscape->setPixmap(defaultImage);
                else
                    ui->label_showLandscape->setPixmap(Winter.at(date.day()%Winter.count())->ImagePath);
            }
        }
        else
        {
            ui->label_showLandscape->setPixmap(Images.at(date.day()%Images.count())->ImagePath);
        }
        ui->label_showLandscape->setScaledContents(true);
        ui->label_showLandscape->show();
    }
}
